package com.ruoyi.h5.roomEval.controller;

import com.dgy.roomEval.domain.RoomEvalItem;
import com.dgy.roomEval.domain.RoomEvalRecord;
import com.dgy.roomEval.service.IRoomEvalItemService;
import com.dgy.roomEval.service.IRoomEvalRecordService;
import com.dgy.roomEval.service.IXsGyFjhBhService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.h5.roomEval.pojo.dto.AddEvalRecordDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * 教学记录Controller
 * 
 * @author ccc
 * @date 2025-02-12
 */
@RestController
@RequestMapping("h5/roomEval")
public class H5RoomEvalController extends BaseController
{

    @Autowired
    private IRoomEvalRecordService roomEvalRecordService;

    @Autowired
    private IRoomEvalItemService roomEvalItemService;

    @Autowired
    private IXsGyFjhBhService xsGyFjhBhService;

    /**
     * 楼栋楼层房间树
     */
    @GetMapping(value = "/getRoomTree")
    public AjaxResult getRoomTree()
    {
        return success(xsGyFjhBhService.getRoomTree());
    }

    /**
     * 新增内务检查记录
     * @return
     */
    @PostMapping(value = "/addEvalRecord")
    public AjaxResult addEvalRecord(@Validated @RequestBody AddEvalRecordDTO addEvalRecordDTO)
    {
        List<RoomEvalItem> answerList = addEvalRecordDTO.getAnswerList();
        // 计算总得分
        BigDecimal totalScore = answerList.stream()
                .filter(item -> item != null && item.getOptionScore() != null)
                .map(RoomEvalItem::getOptionScore)
                .reduce(BigDecimal.ZERO, BigDecimal::add);

        // 添加检查记录
        RoomEvalRecord roomEvalRecord = new RoomEvalRecord();
        roomEvalRecord.setFjhBh(addEvalRecordDTO.getFjhBh());
        roomEvalRecord.setFjName(addEvalRecordDTO.getFjName());
        roomEvalRecord.setScore(totalScore);
        roomEvalRecordService.insertRoomEvalRecord(roomEvalRecord);
        Long recordId = roomEvalRecord.getRecordId();

        // 保存内务检查评分明细
        answerList.forEach(item -> item.setRecordId(recordId));
        roomEvalItemService.insertRoomEvalItemBatch(answerList);
        return success(totalScore);
    }

}
